<template>
<el-dialog class="select-source" :append-to-body="true" :title="data.title" :visible.sync="data.show" width="750px" :close-on-click-modal="false">
    <el-form label-position="right" ref="selectSource" :model="form" :rules="rules" label-width="110px" @submit.native.prevent>
        <el-form-item label="数据源" prop="sourceId">
            <el-select clearable filterable v-model="form.sourceId" placeholder="请选择数据源" @change="changeOrigin">
                <el-option v-for="(item, i) in dataOrigins" :key="i" :label="item.sourceName" :value="item.sourceId"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="数据库名" prop="databaseSchema">
            <el-select clearable filterable v-model="form.databaseSchema" placeholder="请选择数据库名" @change="changeSchemas">
                <el-option v-for="(item, i) in schemas" :key="i" :label="item" :value="item"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="数据表" prop="srcTableName">
            <el-select clearable @change="changeDataOriginsTables" filterable v-model="form.srcTableName" placeholder="请选择数据表">
                <el-option v-for="(item, i) in dataOriginsTables" :key="i" :label="item" :value="item"></el-option>
            </el-select>
        </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="data.show = false">取 消</el-button>
      <el-button type="primary" v-preventReClick="2000" @click="handleConfirm">确 定</el-button>
    </span>
</el-dialog>
</template>
<script>
import {
    mapGetters
} from 'vuex'
export default {
    name: 'DataRead',
    props: {
        data: {
            type: Object
        }
    },
    computed: {
        ...mapGetters({
            projectId: 'common/getProjectId'
        })
    },
    data () {
        return {
            form: {
                sourceId: '',
                srcTableName: '',
                databaseSchema: ''
            },
            dataOrigins: [],
            schemas: [],
            dataOriginsTables: [],
            rules: {
                databaseSchema: [{
                    required: true,
                    message: '请选择数据库名',
                    trigger: 'change'
                }],
                sourceId: [{
                    required: true,
                    trigger: 'change',
                    message: '请选择数据源'
                }],
                srcTableName: [{
                    required: true,
                    trigger: 'change',
                    message: '请选择该数据源下的数据表'
                }]
            }
        }
    },
    created () {
        let defaultValue = this.data.defaultValue
        if (defaultValue.sourceId) {
            this.form = defaultValue
            this.changeOrigin(defaultValue.sourceId, true)
        }
        this.getDataOrigins(defaultValue.databaseSchema)
    },
    methods: {
        handleConfirm () {
            this.$refs['selectSource'].validate((valid) => {
                if (valid) {
                    this.data.show = false
                    this.$emit('save', this.form)
                } else {
                    return false
                }
            })
        },
        getDataOrigins (databaseSchema) {
            const params = {
                projectId: this.projectId,
                pageSize: -1
            }
            this.$post('/dataSource/queryValidDataSources', params).then(res => {
                if (res) {
                    this.dataOrigins = res
                    if (databaseSchema) {
                        this.changeSchemas(databaseSchema, true)
                    }
                }
            })
        },
        // 切换数据表
        changeDataOriginsTables () {
            if (this.isSelectFiled()) {
                this.getPreviewData()
            }
        },
        // 在选择字段之前判断是否已经选择了数据源，scheme，数据表，缺一不可，flag为true的时候给出提示
        isSelectFiled (flag) {
            const { sourceId, databaseSchema, srcTableName } = this.form
            if (!sourceId) {
                flag && this.$message.error('请选择数据源')
                return false
            }
            if (!databaseSchema) {
                flag && this.$message.error('请选择数据库名')
                return false
            }
            if (!srcTableName) {
                flag && this.$message.error('请选择数据表')
                return false
            }
            return true
        },
        // 获取所有字段信息，flag为true的时候是全选字段，否则用回显字段
        getPreviewData () {
            const { databaseSchema, sourceId, srcTableName } = this.form
            const url = '/dataSource/file-upload/previewDataTable'
            const params = {
                projectId: this.projectId,
                sourceId,
                tableName: srcTableName,
                databaseSchema,
                inputColumns: []
            }
            this.$post(url, params).then(res => {
                if (res) {
                    let arr = res.schema.map(v => v.name)
                    this.data.splitKeyList = arr
                    this.data.inputColumns = res.schema
                    this.multipleVal = arr.join(',')
                }
            })
        },
        // 切换schema
        changeSchemas (databaseSchema, flag) {
            if (!flag) {
                this.form.srcTableName = ''
                this.dataOriginsTables = []
            }
            if (databaseSchema) {
                const params = {
                    projectId: this.projectId,
                    sourceId: this.form.sourceId,
                    databaseSchema
                }
                this.$post('/dataSource/queryDataSourceTables', params).then(res => {
                    if (res) {
                        this.dataOriginsTables = res
                        if (flag) {
                            this.changeDataOriginsTables()
                        }
                    }
                })
            }
        },
        // 切换数据源
        changeOrigin (sourceId, flag) {
            if (!flag) {
                this.form.srcTableName = ''
                this.form.databaseSchema = ''
                this.schemas = []
                this.dataOriginsTables = []
            }
            if (sourceId) {
                const params = {
                    projectId: this.projectId,
                    sourceId,
                    pageSize: -1
                }
                this.$post('/dataSource/queryDataSourceSchemas', params).then(res => {
                    if (res) {
                        this.schemas = res
                    }
                })
            }
        }
    }
}
</script>
<style lang="less">
    .select-source {
        .select-field {
            .el-button {
                width: 260px;
            }
        }
    }
</style>
